package cn.edu.scau.cmi.linhongxin.client;

import cn.edu.scau.cmi.linhongxin.simpleFactoryMethod.MeatFactory;
import cn.edu.scau.cmi.linhongxin.simpleFactoryMethod.Produce;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class SimpleFactoryMethodClient extends Application{
	@FXML
	private AnchorPane anchorPane;
	@FXML
	private TextArea creatOutput;
	@FXML
	private Button button;
	@FXML
	private ChoiceBox<String> choiceBox;
	private String nowMeat;
	private final String[] chooseType = {"pork", "beef", "chicken"};
//	public static void main(String[] args) {
//		// TODO Auto-generated method stub
//		MeatFactory pork =Produce.produce("pork");
//		pork.getMeatInfo();//生产pork
//		MeatFactory beef =Produce.produce("beef");
//		beef.getMeatInfo();//生产beef
//		MeatFactory chicken =Produce.produce("chicken");
//		chicken.getMeatInfo();//生产chicken
//
//	}	
//       ChoiceBox choiceBox=new ChoiceBox<>();
	
	@FXML
	public  void initialize() {	
		
		ObservableList<String> list = FXCollections.observableArrayList(chooseType);
		nowMeat = chooseType[0];
		choiceBox.setItems(list);
		choiceBox.setTooltip(new Tooltip("选择肉类"));
		choiceBox.getSelectionModel().selectFirst();
		choiceBox.getSelectionModel().selectedIndexProperty().addListener(
			new ChangeListener<Number>() {
				@Override
				public void changed(ObservableValue<? extends Number> observable, Number oldValue,
						Number newValue) {
					nowMeat = chooseType[newValue.intValue()];
				}
			});
		listenButton();
	}
	public void listenButton() {
		
		button.setOnMouseClicked(new EventHandler<MouseEvent>() {			
			@Override
			public void handle(MouseEvent arg) {
/* 				if(nowMeat.compareTo(chooseType[0]) == 0) {
//					creatOutput.setText("dvdvd");
					return;
				}*/
				creatOutput.clear();
				MeatFactory instance = Produce.produce(nowMeat);
//				System.out.println(instance.getMeatInfo());
//				System.out.println(creatOutput);
				creatOutput.appendText(instance.getMeatInfo()+'\n');
				
			}
		});
		
	}
	private Stage PrimaryStage;
	private AnchorPane rootLayout;
	@Override
	public void start(Stage primaryStage) {
		try {
			this.PrimaryStage = primaryStage;
			this.PrimaryStage.setTitle("简单工厂模式");			
			initRootLayout();		
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void initRootLayout() {
		try {
			FXMLLoader loader = new FXMLLoader();
			loader.setLocation(SimpleFactoryMethodClient.class.getResource("/cn/edu/scau/cmi/linhongxin/simpleFactoryMethod/SimpleFactoryMethodStage.fxml"));
			rootLayout = (AnchorPane) loader.load();		
			Scene scene = new Scene(rootLayout,844,477);
			PrimaryStage.setScene(scene);
			PrimaryStage.show();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		launch(args);
	}
	
}
